package com.coffee.doublesliderseekbar;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.widget.FrameLayout;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

/**
 * Created by coffee on 2021/3/17.
 *
 * @author coffee
 */
public class DoubleSliderSeekBar extends FrameLayout implements OnSeekBarChangeListener {
    private CustomSeekBar leftSeekBar;
    private CustomSeekBar rightSeekBar;
    private int leftIndex = 0;
    private int rightIndex = 100;
    //两个滑块之间的最小距离
    private int interval = 11;
    private OnSeekBarChangeListener onSeekBarChangeListener = null;

    public DoubleSliderSeekBar(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        inflater.inflate(R.layout.double_slider_seek_bar, this);
        initView();
    }

    public void setLeftIndex(int leftIndex) {
        this.leftIndex = leftIndex;
        leftSeekBar.setProgress(leftIndex);
    }

    public void setRightIndex(int rightIndex) {
        this.rightIndex = rightIndex;
        rightSeekBar.setProgress(rightIndex);
    }

    public void setInterval(int interval) {
        this.interval = interval;
    }

    public void setOnSeekBarChangeListener(OnSeekBarChangeListener onSeekBarChangeListener) {
        this.onSeekBarChangeListener = onSeekBarChangeListener;
    }

    private void initView() {
        leftSeekBar = findViewById(R.id.seek_bar_left);
        rightSeekBar = findViewById(R.id.seek_bar_right);

        leftSeekBar.setProgress(leftIndex);
        rightSeekBar.setProgress(rightIndex);

        //拦截seekbar的点击事件
        leftSeekBar.interceptAction(true);
        rightSeekBar.interceptAction(true);

        leftSeekBar.setOnSeekBarChangeListener(this);
        rightSeekBar.setOnSeekBarChangeListener(this);
    }

    /**
     * 拖动条进度改变的时候调用
     */
    @Override
    public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
        if (seekBar == leftSeekBar) {
            leftIndex = Math.min(progress, rightIndex - interval);
            seekBar.setProgress(leftIndex);
        } else if (seekBar == rightSeekBar) {
            rightIndex = Math.max(progress, leftIndex + interval);
            seekBar.setProgress(rightIndex);
        }
        if (onSeekBarChangeListener != null) {
            onSeekBarChangeListener.onProgressChanged(this, leftIndex, rightIndex, fromUser);
        }
    }

    /**
     * 拖动条开始拖动的时候调用
     */
    @Override
    public void onStartTrackingTouch(SeekBar seekBar) {
        if (onSeekBarChangeListener != null) {
            onSeekBarChangeListener.onStartTrackingTouch(this);
        }
    }

    /**
     * 拖动条停止拖动的时候调用
     */
    @Override
    public void onStopTrackingTouch(SeekBar seekBar) {
        if (onSeekBarChangeListener != null) {
            onSeekBarChangeListener.onStopTrackingTouch(this);
        }
    }

    public interface OnSeekBarChangeListener {
        void onStartTrackingTouch(DoubleSliderSeekBar doubleSliderSeekBar);

        void onProgressChanged(DoubleSliderSeekBar doubleSliderSeekBar, int leftIndex, int rightIndex, boolean fromUser);

        void onStopTrackingTouch(DoubleSliderSeekBar doubleSliderSeekBar);
    }
}
